package Askgene::Database::Schema::Project;

use strict;
use warnings;

use base qw/DBIx::Class/;
use Reaction::Class;

use overload '""' => sub { shift->name }, fallback => 1;

has name        => ( isa => 'NonEmptySimpleStr', is => 'rw', required => 1 );
has summary     => ( isa => 'NonEmptySimpleStr', is => 'rw', required => 1 );
has description => ( isa => 'NonEmptyStr',       is => 'rw', required => 1 );
has created => ( isa => 'DateTime', is => 'ro' );
has library_count => (
    isa     => 'PositiveInt',
    is      => 'rw',
    lazy    => '1',
    default => sub { shift->libs->count }
);
has entry_count => (
    isa     => 'PositiveInt',
    is      => 'rw',
    lazy    => '1',
    default => sub { shift->entries->count }
);

__PACKAGE__->load_components(qw/ TimeStamp Core /);
__PACKAGE__->table('projects');
__PACKAGE__->add_columns(
    name => {
        'data_type' => 'varchar',
        'size'      => '50',
    },
    summary => {
        'data_type' => 'varchar',
        'size'      => '255',
    },
    description => {
        'data_type' => 'text',
        'size'      => undef,
    },
    created => {
        'data_type'     => 'timestamp',
        'set_on_create' => '1',
    },
);
__PACKAGE__->set_primary_key('name');

__PACKAGE__->has_many(
    'libs' => 'Askgene::Database::Schema::Library',
    { 'foreign.project_name' => 'self.name' }
);
__PACKAGE__->has_many(
    'entries' => 'Askgene::Database::Schema::Entry',
    { 'foreign.project_name' => 'self.name' }
);

sub store_column {
    my ( $self, $name, $value ) = @_;
    if ( $name eq 'name' ) {
        $value = lc join( '_', split( /\s+/, $value ) );
    }
    $self->next::method( $name, $value );
}

1;
